<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
    String imgSrc = basePath + "resources/images/noImage.jpg";
    session.setAttribute("imgSrc", imgSrc);
%>
<!DOCTYPE html>
<html style="background-color: #fff;">
<head>
    <%@include file="../../../resources/inc/head.jsp" %>
</head>
<body>
<expect-x:tip tipMessage="标识“*”的选项为必填项，其余为选填项。;请按提示信息填写每一个字段" widthClass="full-width-85"/>

<div class="layui-form common-form" lay-filter="layuiadmin-form-contentFriendlyLink"
     id="layuiadmin-form-contentFriendlyLink"
     style="padding: 20px;">
    <div class="layui-form-item">
        <label class="layui-form-label">头像</label>
        <div class="upload-wrapper upload-div " id="layer-photos">
            <div class="upload-item">
                <div class="layui-input-inline">
                    <expect-x:webUploader required="true" pickerId="avatar" isLocal="false" fileList="${permissionUsers.avatar}"/>
                </div>
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <input type="hidden" name="id" value="${permissionUsers.id}">

        <label class="layui-form-label">用户名</label>
        <div class="layui-input-inline">
            <input type="text" name="userName" id="LAY-user-login-userName" lay-verify="nickname|required"
                   placeholder="请输入用户名" class="layui-input" value="${permissionUsers.userName}">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">姓名</label>
        <div class="layui-input-inline">
            <input type="text" name="realName" id="LAY-user-login-realName" lay-verify="nickname|required"
                   placeholder="请输入姓名" class="layui-input" value="${permissionUsers.realName}">
        </div>
    </div>
    <c:if test="${permissionUsers.password==null}">
    <div class="layui-form-item">
        <label class="layui-form-label">密码</label>
        <div class="layui-input-inline">
            <input type="password" name="password" id="LAY-user-login-password" lay-verify="pass|required"
                   placeholder="请输入密码" class="layui-input" >
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">确认密码</label>
        <div class="layui-input-inline">
            <input type="password" name="repass" id="LAY-user-login-repass" lay-verify="required" placeholder="请输入确认密码"
                   class="layui-input" >
        </div>
    </div>
    </c:if>

    <div class="layui-form-item">
        <label class="layui-form-label">手机号</label>
        <div class="layui-input-inline">
            <input type="text" name="phone" id="LAY-user-login-phone" placeholder="请输入手机号"
                   class="layui-input" value="${permissionUsers.phone}">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">邮箱</label>
        <div class="layui-input-inline">
            <input type="text" name="email" value="${permissionUsers.email}"
                   placeholder="请输入邮箱" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item layui-hide">
        <input type="button" lay-submit lay-filter="LAY-submit" id="LAY-submit" value="确认">
    </div>
</div>

<%@include file="../../../resources/inc/footer.jsp" %>

<script>
    layui.config({
        base: '<%=basePath%>resources/lib/layuiAdmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form'], function () {
        var $ = layui.$
            , form = layui.form
    })

    function createSubmit() {
        var userId = "${sessionScope.permissionUser.id}";
        var layer = layui.layer;
        var submitID = 'LAY-submit';
        layui.form.on('submit(' + submitID + ')', function (data) {
            var formData = data.field;
            formData.userId = userId
            //确认密码
            var b = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$/g;
            if (b.test(formData.password)) {
                if (formData.password != formData.repass) {
                    layer.msg("两次密码输入不一致");
                    return;
                }
            } else {
                layer.msg("密码必须包含数字与字母,且必须为八到十六位.");
                return;
            }

          /*  if (!/^1[0-9]{10}/.test(formData.phone)) {
                layer.msg("请输入正确格式的手机号码");
                return;
            }
            if (!/[a-zA-Z0-9]{1,10}@[a-zA-Z0-9]{1,5}\.[a-zA-Z0-9]{1,5}/.test(formData.email)) {
                layer.msg("请输入正确格式的电子邮箱");
                return;
            }*/
            if (formData.id == "" || formData.id == null) {
                delete formData["id"];
                $.ajax({
                    url: '<%=basePath%>manager/permissionUser/create.html',
                    type: "POST",
                    data: formData,
                    dataType: "JSON",
                    success: function (data) {
                        layer.closeAll('loading');
                        if (data.success) {
                            layer.msg("创建成功,1秒后跳转")
                            setTimeout(function () {
                                window.parent.location.reload(); //刷新父页面
                                var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                                parent.layer.close(index);
                            }, 1000);
                        } else {
                            layer.msg(data.message, {icon: 5});
                        }
                    }
                });
            } else {
                $.ajax({
                    url: "<%=basePath%>manager/permissionUser/update/" + formData.id + ".html",
                    type: "POST",
                    data: formData,
                    dataType: "JSON",
                    success: function (data) {
                        layer.closeAll('loading');
                        if (data.success) {
                            layer.msg("修改成功,1秒后跳转");
                            setTimeout(function () {
                                window.parent.location.reload(); //刷新父页面
                                var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                                parent.layer.close(index);
                            }, 1000);
                        } else {
                            layer.msg(data.message, {icon: 5});
                        }
                    }
                });
            }
        })
        $("#" + submitID).trigger('click');
    }
</script>
</body>
</html>
